package com.woniuxy.service.impl;

import com.woniuxy.Exception.serviceException;
import com.woniuxy.dao.DrugDao;
import com.woniuxy.dao.DrugOrderDao;
import com.woniuxy.dao.DrugOrderItemDao;
import com.woniuxy.dao.DrugPeopleDao;
import com.woniuxy.dao.DrugRepertoryDao;
import com.woniuxy.entity.*;
import com.woniuxy.service.DrugAddService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DrugAddServiceImpl implements DrugAddService {

    @Resource
    DrugPeopleDao drugPeopleDao;
    @Resource
    DrugRepertoryDao drugRepertoryDao;
    @Resource
    DrugDao drugDao;
    @Resource
    DrugOrderDao drugOrderDao;
    @Resource
    DrugOrderItemDao drugOrderItemDao;


//    药房人员查询
    @Override
    public List<DrugPeople> selectPeople() {

        List<DrugPeople> drugPeople=drugPeopleDao.selectAll();

        return drugPeople;
    }

//    弹框药品查询
    @Override
    public List<DrugRepertory> selectRepertory(String name, Integer kind) {

        List<DrugRepertory> drugRepertoryList=drugRepertoryDao.selectAll(name,kind);
        for(DrugRepertory tu:drugRepertoryList){

            DrugKind drugKinds= drugDao.selectKing(tu.getKindId());
            tu.setKind(drugKinds.getKindName());
        }
        return drugRepertoryList;
    }

//    药品信息入库
@Transactional(rollbackFor = Exception.class)
    @Override
    public void updateInsterDrug(DrugOrder drugOrder) {

        List<DrugRepertory> drugRepertoryList=null;
    try {
        drugRepertoryList=drugOrder.getDrugRepertory();
        //        添加入库信息
        drugOrderDao.insterDrugOrder(drugOrder);
        DrugOrder drugOrder1=drugOrderDao.selectDrugOrder(drugOrder.getOrderNo(), null);

        //        修改最新药品库存信息
        for (DrugRepertory dr:drugRepertoryList){
            drugRepertoryDao.updateDrug(dr.getRepertoryPrice(),
                    dr.getRepertorySaleprice(),
                    dr.getRepertorySum(),
                    dr.getRepertoryUnit(),
                    dr.getRepertoryDate(),
                    dr.getRepertoryMassdate(),
                    dr.getRepertoryId());
            //        添加订单明细表
            DrugOrderItem drugOrderItem=new DrugOrderItem(null,drugOrder1.getOrderId(),dr.getRepertoryId(),dr.getRepertorySum(),dr.getKindId(),dr.getRepertoryUnit(),0,null,null);
            drugOrderItemDao.insterItem(drugOrderItem);
        }
    } catch (Exception e) {
        throw new serviceException(e);
    }


    }


}
